

#include <stdio.h>
#include <stdlib.h>

struct ListNode{
    int val;
    struct ListNode *next;
};

typedef struct ListNode Node, *pNode;

void print(struct ListNode* p)
{
	struct ListNode* q;
	for(q = p; q != NULL; q = q->next)
		printf("[%d]", q->val);
	printf("\n");
}

void setList(struct ListNode * p, int val)
{
	struct ListNode *q;
	q = p;
	while(q->next != NULL)
		q = q->next;
	
	struct ListNode *tmp = (struct ListNode *)malloc(sizeof(struct ListNode));
	if(tmp == NULL)
		return;
	tmp->val = val;
	tmp->next = q->next;
	q->next = tmp;
}

struct ListNode* swapPairs(struct ListNode* head) {
    struct ListNode* p, *q;
	struct ListNode* tmp;
	p = q = head;
	if(p == NULL || p->next == NULL)
		return head;
	p = p->next;
	for(tmp = q;; )
	{
		/**/
		
		q->next = p->next;
		if(head == q && tmp == q)
			head = p;
		else
			tmp->next = p;
		p->next = q;
		
		/**/
		if(q->next == NULL)
			break;
		else
		{
			tmp = q;
			q = q->next;
			if(q->next == NULL)
				break;
			else
			{
				p = q->next;
			}
		}
	}
	return head;
}

int main(void)
{
	struct ListNode* head;
	int num[] = {1,2,3,4,5,6};
	int val = 1;
	int i;
	head = (struct ListNode *)malloc(sizeof(struct ListNode));
	head->val = num[0];
	head->next = NULL;

	for(i = 1; i < sizeof(num)/sizeof(int); i++)
		setList(head, num[i]);
	print(head);
	print(swapPairs(head));
	return 0;
}